/*******************右侧功能栏********************************/
var rights=document.querySelector('.fixed').children;
var fixop=rights[0];
var poption=rights[1];
var fixlis=fixop.children[0].children;
var poptionlis=poption.children;
for(let i=0;i<fixlis.length-1;i++){
   fixlis[i].index=i;
   fixlis[i].addEventListener('mouseover',function(){
      poption.className="poption";
      for(let j=0;j<poptionlis.length;j++){
         fixlis[j].className="";
         poptionlis[j].style.display="none";
      }
      poptionlis[this.index].style.top+=60*i+'px';
      poptionlis[this.index].style.display='block';
   })
}
fixop.addEventListener('mouseout',function(){
   poption.className='poption hide'
})
poption.addEventListener('mouseout',function(){
   poption.className='poption hide'
})
//回到顶部效果
$(fixlis[4]).on('click',function(){
$('body,html').animate({
   scrollTop:0,
},300)
});
/***********未登录状态跳转至登录页****************/
let login=document.querySelector('.false>button');
login.onclick=function(){
   location.href="login.html"
}
/***********************************************/
    // 事件委托实现数量的加减
    console.log(document.querySelector('.shoppingBar'));
    document.querySelector('.shoppingBar').addEventListener('click',function(event){
      let e = event||window.event;
      // 获取到事件源中的文本内容 并且去掉空格
      let content = e.target.innerHTML.trim();
      let number,price;
      switch (content) {
          case '-':
              //获取到当前数量
              number= parseInt(e.target.nextElementSibling.innerHTML)
              if(number<=1){
                  alert('数量最少1件');
                  return false;
              }
              // 数量减1
              e.target.nextElementSibling.innerHTML = --number;
              //获取到单价
              price =parseFloat(e.target.parentNode.nextElementSibling.innerHTML);
              // 计算金额
              e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = price * number;
              total();
              break;
          case '+':
              //获取到当前数量
              number = parseInt(e.target.previousElementSibling.innerHTML);
              // 数量减1
              e.target.previousElementSibling.innerHTML = ++number;
              //获取到单价
              price =parseFloat(e.target.parentNode.nextElementSibling.innerHTML);
              // 计算金额
              e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = price * number;
              total();
              break;
          case '删除':
              e.target.parentNode.parentNode.remove();
              total();
              break;
          default:
              if(e.target.className == 'input'){
                  // 勾选操作
                  total();
                  // 是否选中全选
                  if(document.querySelectorAll('.input:checked').length ==document.querySelectorAll('.input').length){
                      document.querySelector('input').checked = true;
                  }else{
                      document.querySelector('input').checked = false;
                  }
              }
              break;
      }
  })
  // 计算总数量信息
  function total(){
      // 查找被选中的input标签
      let inputs = document.querySelectorAll('.input:checked');
      let totalNumber=0,totalMoney=0;
      for(let i =0;i<inputs.length;i++){
          // 获取到当前勾选input的上上级tr标签
          let patrentTr = inputs[i].parentNode.parentNode
          // 计算总数量
          totalNumber += parseInt(patrentTr.children[2].children[1].textContent);
          // 计算总的金额
          totalMoney += parseFloat(patrentTr.children[4].textContent);
      }
      document.querySelectorAll('.buy span')[0].textContent = totalNumber;
      document.querySelectorAll('.buy span')[1].textContent = '¥'+totalMoney;
  }